package com.zzuli.fitness.controller;

import com.zzuli.fitness.entity.Coach;
import com.zzuli.fitness.entity.CoachVo;
import com.zzuli.fitness.entity.Course;
import com.zzuli.fitness.service.CoachService;
import com.zzuli.fitness.service.CourseService;
import com.zzuli.fitness.service.GymService;
import com.zzuli.fitness.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Api(tags = "教练接口")
@RestController
@RequestMapping("/coach")
@Slf4j
public class CoachController {
    @Autowired
    private CoachService coachService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private GymService gymService;

    @ApiOperation(value = "教练列表")
    @GetMapping("/findAllCoach")
    public Result findAllCoach() {
        List<Coach> list = coachService.list();
        for (int i=0;i<list.size();i++) {
            Coach coach = list.get(i);
            String url = "http://127.0.0.1:8090/fitness" + coach.getImage();
            coach.setImage(url);
            list.set(i,coach);
        }
        return Result.ok(list);
    }

    @ApiOperation(value = "根据ID查看教练信息")
    @GetMapping("/findCoachInfoById")
    public Result findCoachInfoById(@RequestParam Integer id) {
        Coach coach = coachService.getById(id);
        CoachVo coachVo = new CoachVo();
        coachVo.setId(coach.getId());
        coachVo.setName(coach.getName());
        coachVo.setAge(coach.getAge());
        coachVo.setSex(coach.getSex());
        coachVo.setPhone(coach.getPhone());
        coachVo.setInfo(coach.getInfo());
        String img = "http://127.0.0.1:8090/fitness" + coach.getImage();
        coachVo.setImage(img);
        coachVo.setStatus(coach.getStatus());
        coachVo.setDirection(coach.getDirection());
        coachVo.setStyle(coach.getStyle());
        Course course = courseService.selectPrivateCourse(id);
        coachVo.setCourseName(course.getName());
        coachVo.setCourseId(course.getId());
        String courseImg = "http://127.0.0.1:8090/fitness" + course.getImage();
        coachVo.setCourseImage(courseImg);
        coachVo.setPrice(course.getPrice());
        coachVo.setAddress(gymService.getById(1).getAddress());
        return Result.ok(coachVo);
    }

    @ApiOperation(value = "根据教练名查看教练信息")
    @GetMapping("/findCoachInfoByName")
    public Result findCoachInfoByName(@RequestParam String name) {
        Coach coach = coachService.getByName(name);
        String url = "http://127.0.0.1:8090/fitness" + coach.getImage();
        coach.setImage(url);
        return Result.ok(coach);
    }
}
